package com.mallcai.bigdata.ladon.service.export.impl.handler;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.mallcai.bigdata.ladon.service.export.impl.ExportContext;
import com.mallcai.bigdata.ladon.tools.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;

/**
 * described : 将本地文件保存至OSS
 *
 * @author CaKeDou
 * @date 2019-09-26 21:52
 * Modify:
 */
@Slf4j
@Component
@Order(800)
public class SaveOssHandler extends AbstractExportPostHandler {
    @Override
    public void postInternal(ExportContext context) {
        if (!context.getExportStrategy().isSaveToOss()) {
            return;
        }
        try {
            String downLoadUrl = OssUtil.defaultUpload(context.getFileName(), new FileInputStream(context.getFilePath()));
            context.setDownloadUrl(downLoadUrl);
        } catch (Exception e) {
            log.error("存至OSS出错", e);
            throw new BusinessException("存至OSS出错");
        }
    }
}
